/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TiendaView.java
 *
 * Created on 05/12/2009, 10:24:16 PM
 */

package vista;

import controlador.ITiendaController;
import modelo.ITienda;
import modelo.ITiendaModel;
/**
 *
 * @author LEONIC
 */
public class TiendaView extends javax.swing.JFrame implements ITiendaView {

    private ITiendaModel model;
    private ITiendaController controller;

    private TiendaTableModel tableModel;
    private JPanelTienda panelTienda;

    /** Creates new form TiendaView */
    public TiendaView() {
        tableModel=new TiendaTableModel();
        initComponents();

        this.panelTienda=new JPanelTienda(this);
        this.panelabajo.add(this.panelTienda);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTableTienda = new javax.swing.JTable();
        panelabajo = new javax.swing.JPanel();

        setTitle("Gestión de tiendas");

        jTableTienda.setModel(tableModel);
        jTableTienda.setUpdateSelectionOnSort(false);
        jTableTienda.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableTiendaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTableTienda);

        panelabajo.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        panelabajo.setLayout(new javax.swing.BoxLayout(panelabajo, javax.swing.BoxLayout.LINE_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(panelabajo, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 538, Short.MAX_VALUE))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(panelabajo, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(27, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTableTiendaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableTiendaMouseClicked
        TiendaTableModel ctm=(TiendaTableModel)this.jTableTienda.getModel();
        ITienda tienda=ctm.getRow(this.jTableTienda.getSelectedRow());
        this.panelTienda.setTienda(tienda);
}//GEN-LAST:event_jTableTiendaMouseClicked

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TiendaView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTableTienda;
    private javax.swing.JPanel panelabajo;
    // End of variables declaration//GEN-END:variables

    public ITiendaController getController() {
        return controller;
    }

    public void setController(ITiendaController controller) {
        this.controller=controller;
    }

    public ITiendaModel getModel() {
        return model;
    }

    public void setModel(ITiendaModel model) {
        this.model=model;
    }

    public void dataModelChanged() {
        refresh();
    }

    public void display() {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                setVisible(true);
                refresh();
            }
        });
    }

    protected void refresh() {
        this.tableModel.setTiendas(getModel().obtenerTienda());
    }

    protected void fireNuevoTiendaGesture(int ID, String direccion, int cp, int telefono, int capacidad) {
        getController().nuevoTiendaGesture(ID, direccion, cp, telefono, capacidad);
    }

    protected void fireActualizaTiendaGesture(int ID, String direccion, int cp, int telefono, int capacidad) {
        getController().actualizaTiendaGesture(ID, direccion, cp, telefono, capacidad);
    }

    protected void fireasignarTareasTiendaGesture(int ID, String direccion, int cp, int telefono, int capacidad) {
        getController().asignarTareasTiendaGesture(ID, direccion, cp, telefono, capacidad);
    }

    protected void fireBorraTiendaGesture(int ID){
        getController().borraTiendaGesture(ID);
    }

    public void deshabilitarTareas(){
        this.panelTienda.deshaTareas();
    }

    public void deshabilitarEncargados(){
        this.panelTienda.deshaEncargado();
    }
}
